package models;

import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ModelUtility  implements Serializable
{
	/**
	 * Returns true iff the grid locations are adjacent to each other.
	 * This assumes that even number rows are in-set compared to odd number rows;  
	 * @param x1 the x of the first grid location.
	 * @param y1 the y of the first gris location.
	 * @param x2 the x of the second grid location.
	 * @param y2 the y of the second grid location.
	 * @return
	 */
	public static boolean isAdjacent(int x1,int y1,int x2, int y2)
	{
		int dx = x1-x2;
		int dy = y1-y2;

		if(dx == 0 && Math.abs(dy) == 1)
			return true;
		if(Math.abs(dx) == 1 && x1%2 == 0 && (dy == 1 || dy == 0))
			return true;
		if(Math.abs(dx) == 1 && x1%2 == 1 && (dy == -1 || dy == 0))
			return true;
		return false;
	}
	
	public static boolean isAdjacent(Point p1, Point p2)
	{
		return isAdjacent(p1.x,p1.y,p2.x,p2.y);
	}

	/**
	 * Returns a list of all possible start locations on the given board.
	 */
	public static List<Location> getStartLocations(Board board)
	{
		List<Location> startLocations= new ArrayList<Location>();
		for(int x = 0;x< board.getWidth();x++)
			for(int y = 0; y<board.getHeight();y++)
				if(board.getLocation(x, y).isStartLocation())
					startLocations.add(board.getLocation(x, y));

		return startLocations;
	}
	
	/**
	 * Returns the location of the given creature on the given board.
	 * @param board
	 * @param creature
	 * @return
	 */
	public static Point getPosition(Board board,Creature creature)
	{
		for(int x = 0; x < board.getWidth(); x++)
			for(int y = 0; y < board.getHeight(); y++)
				if(board.getLocation(x, y).getCreature() == creature)
					return new Point(x,y);
		return null;
	}
}
